package com.business.commons.utils.net;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.alibaba.druid.support.json.JSONUtils;
import com.business.commons.utils.JsonUtils;
import com.business.model.weixin.Callbackip;
import com.business.model.weixin.WxAssessToken;

/**
 * https通信
 * 
 * @author limt
 *
 */
public class HttpPostConnect {
  //private String url = "http://172.198.196.110:8090/Hlinsiis";
	private static String  url = "http://124.160.76.183:20443/Hlinsiis";
	private static String charset = "UTF-8";
	private HttpsClientUtil httpsClientUtil = null;

	
	
	
	//静态加载
	static {
		Properties props = null;
		try {
			props = new Properties();
			String file = "/opt/wondersgroup/ltcins-net-communication-provider/communication_url.properties";
			props.load(new FileInputStream(file));
			if(props.get("communication.url") != null){
				url = (String) props.get("communication.url") ;
			}
			else{
				throw new FileNotFoundException(file);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

	
	
	
	
	public static HttpPostConnect getConnect() {
		return new HttpPostConnect();
	}
	
	
	
	
	
	

	public HttpPostConnect() {
		httpsClientUtil = new HttpsClientUtil();
	}

	/**
	 * @return the url
	 */
	public static String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public static void setUrl(String url) {
		HttpPostConnect.url = url;
	}

	/**
	 * 获取数据
	 * 
	 * @param function
	 *            功能地址
	 * @param createMap
	 *            參數
	 * @return
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public String getDataPost(String function, Object obj) throws IllegalArgumentException, IllegalAccessException {
		String httpOrgCreateTest = url + function;
		Map<String, String> createMap = this.objToMap(obj);
		String httpOrgCreateTestRtn = httpsClientUtil.doPost(httpOrgCreateTest, createMap, charset);
		return httpOrgCreateTestRtn;
	}

	/**
	 * 获取数据
	 * 
	 * @param function
	 *            功能地址
	 * @param createMap
	 *            參數
	 * @return
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public String getDataPost(String function, Map<String, String> createMap)
			throws IllegalArgumentException, IllegalAccessException {
		String httpOrgCreateTest = url + function;
		if (null == createMap) {
			createMap = new HashMap<String, String>();
		}
		String httpOrgCreateTestRtn = httpsClientUtil.doPost(httpOrgCreateTest, createMap, charset);
		return httpOrgCreateTestRtn;
	}

	public void test() {
		String httpOrgCreateTest = url + "httpOrg/create";
		Map<String, String> createMap = new HashMap<String, String>();
		createMap.put("authuser", "*****");
		createMap.put("authpass", "*****");
		createMap.put("orgkey", "****");
		createMap.put("orgname", "****");
		String httpOrgCreateTestRtn = httpsClientUtil.doPost(httpOrgCreateTest, createMap, charset);
		System.out.println("result:" + httpOrgCreateTestRtn);
	}

	public static void main(String[] args) {
		/*HttpPostConnect main = new HttpPostConnect();
		main.test();*/
		HttpsClientUtil https = new HttpsClientUtil();
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9fe88a4e1b6c3aa2&secret=3070e703ab5d7a94ffabe5aff30e7579";
		String result = https.doGet(url, null);
		WxAssessToken wxAssessToken = JsonUtils.parse(result, WxAssessToken.class);
		System.out.println("");
		System.out.println("access_token:"+wxAssessToken.getAccess_token() +"\n expires_in:"+ wxAssessToken.getExpires_in());
		String url2 = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=4_nuoqsIIdMst6Qm77Rde8A-Z8Vs0UdhqfxY2UafniltqGaORk89yCjqk-2OshXjPzxUbnL6jfyMGw4GsaCjGukfk-CsXpy_ACnFaLL24oNFSOkiYuVKfGBFg0QqfyrAsD5K732ILHAniUAZemTOMjAGANIG";
		String result2 = https.doGet(url2, null);
		Callbackip callbackip = JsonUtils.parse(result2, Callbackip.class);
		for(String sx : callbackip.getIp_list()) {
			System.out.println("server ip:"+sx);
		}
	}

	/**
	 * 转换
	 * 
	 * @author limt
	 * @param obj
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public Map<String, String> objToMap(Object obj) throws IllegalArgumentException, IllegalAccessException {
		Map<String, String> createMap = new HashMap<String, String>();
		if (null == obj) {
			return createMap;
		}
		// 得到类对象
		Class userCla = (Class) obj.getClass();
		/*
		 * 得到类中的所有属性集合
		 */
		Field[] fs = userCla.getDeclaredFields();
		for (int i = 0; i < fs.length; i++) {
			Field f = fs[i];
			if("serialVersionUID".equals(f.getName())){
				continue;
			}
			f.setAccessible(true); // 设置些属性是可以访问的
			Object val = f.get(obj);// 得到此属性的值
			if(null != val){
				createMap.put(f.getName(), val.toString());
			}
		}
		return createMap;
	}
}
